package org.stratusdb.funsql.execute;

import java.io.Serializable;
import java.util.Vector;

import org.stratusdb.Config;
import org.stratusdb.types.TableType;
import org.stratusdb.types.TupleType;
import org.stratusdb.error.Error;

public abstract class AbstractOperator implements Serializable{

	private static final long serialVersionUID = -5580853707848919989L;

	protected Error lastError = Error.NO_ERROR;	
	
	public Error getLastError() {
		return lastError;
	}

	public abstract Error open();
	
	public abstract TableType next();
	
	public abstract Error close();
	
	/**
	 * Executes the Projection for a StratusDB - Executeoperator
	 *  
	 * @param tt Result - Table from an operator
	 * @param attr projection list
	 * @return table with the right columns
	 */
	protected TableType filter(TableType tt, Vector<Integer> attr) {
		if(tt == null) return null;
		if(attr == null) return tt;
		TableType retTab = new TableType(Config.EXECUTE_MAX_FETCHSIZE);
		int c = 0;
		for(TupleType tuty : tt.getTuples()) {
			if(tuty == null) break;
			TupleType erg = new TupleType(attr.size());
			int b=0;
			for(int i : attr) {
				erg.setValue(b++,tuty.getValue(i-1));
			}
			retTab.setTuple(c++,erg);
		}
		return retTab;		
	}
	
	public abstract String toString();
}
